package com.dragon.mmochat.model.bet;

import java.awt.Color;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dragon.mmochat.MmochatConstant;
import com.dragon.mmochat.message.Mmochat_17_5S;
import com.dragon.mmochat.message.Mmochat_17_7S;
import com.dragon.mmochat.model.enumType.MmochatBetState;
import com.dragon.mmochat.model.enumType.MmochatBetType;
import com.dragon.mmochat.model.enumType.MmochatBigMoneyCostType;
import com.dragon.mmochat.model.enumType.MmochatBigMoneyEarnType;
import com.dragon.mmochat.model.enumType.MmochatSmallMoneyCostType;
import com.dragon.mmochat.model.enumType.MmochatSmallMoneyEarnType;
import com.dragon.mmochat.model.role.MmochatPlayer;
import com.dragon.mmochat.service.MmochatDao;
import com.dragon.mmochat.service.MmochatFriendService;
import com.dragon.mmochat.service.MmochatMainService;
import com.dragon.mmochat.service.MmochatStatisticsService;
import com.dragon.mmochat.util.MmochatUtil;

public class MmochatBet {
	private static Logger log = LoggerFactory.getLogger(MmochatBet.class);

	private ScheduledExecutorService betThread = Executors
			.newScheduledThreadPool(1);

	// 赌场类型
	private MmochatBetType betType;

	// 4个怪物的赔率及命中机率
	private final int betNum = 4;
	private MmochatBetRate[] betRates = new MmochatBetRate[betNum];

	// 玩家的下注情况
	private Map<Integer, MmochatBetInfo> betMap = new ConcurrentHashMap<Integer, MmochatBetInfo>();

	// 在赌盘界面的玩家
	private Map<Integer, Integer> playersInBet = new ConcurrentHashMap<Integer, Integer>();

	// 赌场状态
	private MmochatBetState betRoomState = MmochatBetState.未启动状态;

	// 下注时间(秒)
	private int betSetMoneyTime = 590; // 9分50秒
	// 冻结下注时间(秒)
	private int betFreezeSetMoneyTime = 10;

	// 赛跑总时间(100ms)
	private int totalRunTime = 100;

	// 跑道长度(px，折返跑总长度)
	// 总长度不能低于maxTimesOfChangeSpeed*minPathLenPerSection
	private int totalPath = 360;
	private int minPathLenPerSection = 40; // 每段长度最小值
	private int minSpeed = 2; // 最小速度
	private int maxSpeed = 6; // 最大速度
	private int minTimesOfChangeSpeed = 2; // 最小变速次数
	private int maxTimesOfChangeSpeed = 4; // 最大 变速次数

	// 状态变更的时刻
	private long stateChangeTick = System.currentTimeMillis();

	private BetStart betRun = new BetStart(this);

	public MmochatBet(MmochatBetType betType, int delaySecond) {
		this.betType = betType;
		for (int i = 0; i < betNum; i++) {
			betRates[i] = new MmochatBetRate();
		}
		betThread.schedule(betRun, delaySecond, TimeUnit.SECONDS);
	}

	// 赌场状态变更
	private void changeBetState(MmochatBetState newState) {
		betRoomState = newState;
		stateChangeTick = System.currentTimeMillis();
	}

	// 赌场运行
	class BetStart implements Runnable {
		MmochatBet bet;

		public BetStart(MmochatBet bet) {
			this.bet = bet;
		}

		@Override
		public void run() {
			try {
				switch (betRoomState) {
				case 未启动状态: {
					initNewBetGame();
					changeBetState(MmochatBetState.可以下注状态);
					// 通知所有在赌盘界面的玩家
					try {
						for (Integer roleId : playersInBet.values()) {
							MmochatPlayer me = MmochatMainService.players
									.get(roleId);
							if (me != null) {
								// 下发开始下注协议，告诉终端赔率
								Mmochat_17_5S pack = new Mmochat_17_5S();
								pack.setBet(bet);
								pack.setMe(me);
								pack.setSystemMsg(getBetSystemMsg());
								MmochatUtil.sendCommonPack(me, pack);
							}
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
					// 可以下注状态持续betSetMoneyTime秒
					betThread.schedule(betRun, betSetMoneyTime,
							TimeUnit.SECONDS);
					break;
				}
				case 可以下注状态: {
					changeBetState(MmochatBetState.冻结下注状态);
					try {
						for (Integer roleId : playersInBet.values()) {
							MmochatPlayer me = MmochatMainService.players
									.get(roleId);
							if (me != null) {
								// 下发停止下注协议
								Mmochat_17_5S pack = new Mmochat_17_5S();
								pack.setBet(bet);
								pack.setMe(me);
								pack.setSystemMsg(getBetSystemMsg());
								MmochatUtil.sendCommonPack(me, pack);
							}
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
					// 冻结下注状态持续betFreezeSetMoneyTime秒
					betThread.schedule(betRun, betFreezeSetMoneyTime,
							TimeUnit.SECONDS);
					break;
				}
				case 冻结下注状态: {
					long t = 0;
					if (MmochatConstant.testBetTimeOut) {
						t = System.currentTimeMillis();
						log.error("开始开盘:" + betType.toString());
					}
					changeBetState(MmochatBetState.开盘状态);
					// 开盘操作
					String extraMsg = "";
					int hitIndex = 0; // 第几个动物中奖
					boolean success = true;
					String recordInfo = "\n\n--------------------------------------\n";
					SimpleDateFormat dateFormat = new SimpleDateFormat(
							"yyyy-MM-dd HH:mm:ss");
					String dateNowStr = dateFormat.format(new Date());
					recordInfo += "时间:" + dateNowStr + "\n";

					try {
						int value = MmochatUtil.getRandomValue(100);
						int curValue = 0;
						for (int i = 0; i < betNum; i++) {
							curValue += betRates[i].getRate();
							if (value < curValue) {
								hitIndex = i;
								break;
							}
						}
						recordInfo += "中奖:第" + hitIndex + "个\n";
						recordInfo += "赔率:";
						for (int i = 0; i < betNum; i++) {
							recordInfo += i + ":" + betRates[i].getAward()
									+ " ";
						}
						recordInfo += "\n";
					} catch (Exception e) {
						e.printStackTrace();
						// 开盘失败
						success = false;
						extraMsg += "系统:开盘失败!异常代码：01\n";
						recordInfo += "开奖失败:" + e.getMessage();
					}
					Map<Integer, String> privateExtraMsg = new ConcurrentHashMap<Integer, String>();
					String noSmallMoneyHint = "系统:下注失败，开盘时您的金钱不足以支付压注资金。";
					String noBigMoneyHint = "系统:下注失败，开盘时您的元宝不足以支付压注资金。";
					if (success) {
						String winInfo = "开盘情况:";
						String offlineRoles = "下线导致下注失败的roleId:";
						String noMoneyRoles = "金钱不足下注失败的roleId:";
						try {
							// addSmallMoney, addBigMoney, roleId
							Map<Integer, Object[]> awardMap = new ConcurrentHashMap<Integer, Object[]>();
							Map<Integer, MmochatPlayer> playersBak = new ConcurrentHashMap<Integer, MmochatPlayer>(
									MmochatMainService.players);
							for (MmochatBetInfo info : betMap.values()) {
								// 钱是否足够下注
								int roleId = info.getRoleId();
								MmochatPlayer me = playersBak.get(roleId);
								if (me == null) {
									// 如果下线、则下注失败
									betMap.remove(roleId);
									offlineRoles += roleId + ",";
									continue;
								}
								if (betType == MmochatBetType.游戏币赌场) {
									if (me.getSmall_money() < info
											.getTotalBetMoney()) {
										// 游戏币不够，下注失败
										if (playersInBet.containsKey(roleId)) {
											// 在赌盘界面
											privateExtraMsg.put(me.getRoleId(),
													noSmallMoneyHint);
										} else {
											// #键好友消息
											MmochatFriendService
													.sendSystemMsgToPersonByFriendMessage(
															me,
															"由于您当前金钱不足以支付<宠物赛跑金钱场>的压注资金，"
																	+ "故此次压注自动取消。");
										}
										betMap.remove(roleId);
										noMoneyRoles += roleId + ",";
										continue;
									}

									// 结算奖励
									int awardMoney = info.getAwardMoney(
											hitIndex, betRates[hitIndex]
													.getAward(), (int) me
													.getSmall_money());
									awardMap.put(info.getRoleId(),
											new Object[] { awardMoney, 0,
													info.getRoleId() });
									winInfo += "roleId:" + info.getRoleId()
											+ ",name:" + me.getName()
											+ ",awardMoney:" + awardMoney
											+ "；压注情况:{";
									for (Entry<Integer, Integer> entry : info
											.getBetMoneyMap().entrySet()) {
										winInfo += entry.getKey() + ":"
												+ entry.getValue() + " ";
									}
									winInfo += "}\n";
								} else if (betType == MmochatBetType.元宝赌场) {
									if (me.getBig_money() < info
											.getTotalBetMoney()) {
										// 元宝不够，下注失败
										if (playersInBet.containsKey(roleId)) {
											// 在赌盘界面
											privateExtraMsg.put(me.getRoleId(),
													noBigMoneyHint);
										} else {
											// #键好友消息
											MmochatFriendService
													.sendSystemMsgToPersonByFriendMessage(
															me,
															"由于您当前元宝不足以支付<宠物赛跑元宝场>的压注资金，"
																	+ "故此次压注自动取消。");
										}
										betMap.remove(roleId);
										noMoneyRoles += roleId + ",";
										continue;
									}

									// 结算奖励
									int awardMoney = info.getAwardMoney(
											hitIndex, betRates[hitIndex]
													.getAward(), (int) me
													.getBig_money());
									awardMap.put(info.getRoleId(),
											new Object[] { 0, awardMoney,
													info.getRoleId() });

									winInfo += "roleId:" + info.getRoleId()
											+ ",name:" + me.getName()
											+ ",awardMoney:" + awardMoney
											+ "；压注情况:{";
									for (Entry<Integer, Integer> entry : info
											.getBetMoneyMap().entrySet()) {
										winInfo += entry.getKey() + ":"
												+ entry.getValue() + " ";
									}
									winInfo += "}\n";
								}
							}
							recordInfo += winInfo + "\n";
							recordInfo += offlineRoles + "\n";
							recordInfo += noMoneyRoles + "\n";

							long t2 = 0;
							if (MmochatConstant.testBetTimeOut) {
								t2 = System.currentTimeMillis();
							}
							boolean sqlSuccess = true;
							try {
								// 统一执行一次数据库批量操作
								List<Object[]> argList = new ArrayList<Object[]>();
								for (Object[] args : awardMap.values()) {
									argList.clear();
									argList.add(args);
									try {
										MmochatDao.batchUpdateMoney(argList);
									} catch (Exception e) {
										e.printStackTrace();
									}
								}
							} catch (Exception e) {
								e.printStackTrace();
								// 开盘失败
								sqlSuccess = false;
								extraMsg += "系统:数据库忙,本次开盘失败!\n";
								recordInfo += "数据库操作失败\n";
							}
							if (MmochatConstant.testBetTimeOut) {
								log.error("batchUpdateMoney cost time:"
										+ (System.currentTimeMillis() - t2)
										+ "ms");
							}

							if (sqlSuccess) {
								// 生成比赛数据
								List<MmochatBetPathSection> pathData = createPath(hitIndex);

								// 给在赌盘的玩家下发开盘信息
								playersBak.clear();
								playersBak = new ConcurrentHashMap<Integer, MmochatPlayer>(
										MmochatMainService.players);
								for (Integer roleId : playersInBet.values()) {
									MmochatPlayer me = playersBak.get(roleId);
									if (me != null) {
										// 下发开盘信息
										int money = 0;
										Object[] moneyParam = awardMap
												.get(roleId);
										if (moneyParam != null) {
											if (betType == MmochatBetType.游戏币赌场) {
												Integer smallMoney = (Integer) moneyParam[0];
												if (smallMoney != null) {
													money = smallMoney;
												}
											} else if (betType == MmochatBetType.元宝赌场) {
												Integer bigMoney = (Integer) moneyParam[1];
												if (bigMoney != null) {
													money = bigMoney;
												}
											}
										}
										Mmochat_17_7S pack = new Mmochat_17_7S();
										pack.setBet(bet);
										pack.setMe(me);
										pack.setPathSections(pathData);
										pack.setMoneyAward(money);
										MmochatUtil.sendCommonPack(me, pack);
									}
								}

								String addMoneyInfo = "内存更新奖励并下发:";
								String offlineLog = "离线玩家,奖励未更新内存和下发roleId:";
								String errorLog = "";

								// 给在线玩家内存中增加钱
								for (Object[] moneyParam : awardMap.values()) {
									if (moneyParam == null) {
										errorLog += "moneyParam is null;\n";
										continue;
									}
									Integer r = (Integer) moneyParam[2];
									if (r == null) {
										errorLog += "moneyParam[2] is null;\n";
										continue;
									}
									int roleId = r;
									MmochatPlayer me = MmochatMainService
											.getRoleFromBuffer(roleId);
									if (me != null) {
										Integer smallMoney = (Integer) moneyParam[0];
										if (smallMoney != null
												&& smallMoney != 0) {
											me
													.addAndUpdateSmall_money(smallMoney);
											if (smallMoney > 0) {
												MmochatStatisticsService
														.addSmallMoneyEarn(
																MmochatSmallMoneyEarnType.游戏币赌场,
																smallMoney);
											} else {
												MmochatStatisticsService
														.addSmallMoneyCost(
																MmochatSmallMoneyCostType.游戏币赌场,
																smallMoney);
											}
											addMoneyInfo += "roleId:" + roleId
													+ ",name:" + me.getName()
													+ ",awardMoney:"
													+ smallMoney + "\n";

											// 通知不在赌盘界面的玩家开奖结果
											if (!playersInBet
													.containsKey(roleId)) {
												// #键好友消息
												String msg = "本次宠物赛跑金钱场胜利者为";
												switch (hitIndex) {
												case 0:
													msg += MmochatUtil
															.wrapColor("枯树精",
																	Color.green);
													break;
												case 1:
													msg += MmochatUtil
															.wrapColor("长尾",
																	Color.green);
													break;
												case 2:
													msg += MmochatUtil
															.wrapColor("仙姑",
																	Color.green);
													break;
												case 3:
													msg += MmochatUtil
															.wrapColor("雪妖",
																	Color.green);
													break;
												default:
													break;
												}
												if (smallMoney > 0) {
													msg += ",您净赢"
															+ MmochatUtil
																	.getColorMoney(smallMoney)
															+ "文钱。";
												} else {
													msg += ",您输了"
															+ MmochatUtil
																	.getColorMoney(-1
																			* smallMoney)
															+ "文钱。";
												}
												MmochatFriendService
														.sendSystemMsgToPersonByFriendMessage(
																me, msg);
											}
										}
										Integer bigMoney = (Integer) moneyParam[1];
										if (bigMoney != null && bigMoney != 0) {
											me.addBig_money(bigMoney);
											if (bigMoney > 0) {
												MmochatStatisticsService
														.addBigMoneyEarn(
																MmochatBigMoneyEarnType.元宝赌场,
																bigMoney);
											} else {
												MmochatStatisticsService
														.addBigMoneyCost(
																MmochatBigMoneyCostType.元宝赌场,
																bigMoney);
											}
											addMoneyInfo += "roleId:" + roleId
													+ ",name:" + me.getName()
													+ ",awardMoney:" + bigMoney
													+ "\n";
											// 通知不在赌盘界面的玩家开奖结果
											if (!playersInBet
													.containsKey(roleId)) {
												// #键好友消息
												String msg = "本次宠物赛跑元宝场胜利者为";
												switch (hitIndex) {
												case 0:
													msg += MmochatUtil
															.wrapColor("枯树精",
																	Color.green);
													break;
												case 1:
													msg += MmochatUtil
															.wrapColor("长尾",
																	Color.green);
													break;
												case 2:
													msg += MmochatUtil
															.wrapColor("仙姑",
																	Color.green);
													break;
												case 3:
													msg += MmochatUtil
															.wrapColor("雪妖",
																	Color.green);
													break;
												default:
													break;
												}
												if (bigMoney > 0) {
													msg += ",您净赢"
															+ MmochatUtil
																	.wrapColor(
																			bigMoney,
																			Color.cyan)
															+ "元宝。";
												} else {
													msg += ",您输了"
															+ MmochatUtil
																	.wrapColor(
																			-1
																					* bigMoney,
																			Color.cyan)
															+ "元宝。";
												}
												MmochatFriendService
														.sendSystemMsgToPersonByFriendMessage(
																me, msg);
											}
										}
									} else {
										offlineLog += roleId + ",";
									}
								}
								recordInfo += addMoneyInfo + "\n";
								recordInfo += offlineLog + "\n";
								recordInfo += errorLog;
							}
						} catch (Exception e) {
							e.printStackTrace();
							recordInfo += "发生异常:" + e.getMessage() + "\n";
						}
					}

					try {
						dateFormat = new SimpleDateFormat("yyyy-MM-dd");
						dateNowStr = dateFormat.format(new Date());
						if (betType == MmochatBetType.游戏币赌场) {
							dateNowStr = "small-" + dateNowStr;
						} else if (betType == MmochatBetType.元宝赌场) {
							dateNowStr = "big-" + dateNowStr;
						}

						MmochatUtil.writeToFile(dateNowStr, recordInfo);
					} catch (Exception e1) {
						e1.printStackTrace();
					}

					// 开盘操作完成后,转为可以下注状态
					initNewBetGame();
					changeBetState(MmochatBetState.可以下注状态);
					try {
						for (Integer roleId : playersInBet.values()) {
							MmochatPlayer me = MmochatMainService.players
									.get(roleId);
							if (me != null) {
								// 下发开始下注协议，告诉终端赔率
								Mmochat_17_5S pack = new Mmochat_17_5S();
								pack.setBet(bet);
								pack.setMe(me);
								String msg = extraMsg;
								String hint = privateExtraMsg.get(me
										.getRoleId());
								if (hint != null) {
									msg += hint;
								}
								msg += getBetSystemMsg();
								pack.setSystemMsg(msg);
								MmochatUtil.sendCommonPack(me, pack);
							}
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
					// 可以下注状态持续betSetMoneyTime秒
					betThread.schedule(betRun, betSetMoneyTime,
							TimeUnit.SECONDS);

					if (MmochatConstant.testBetTimeOut) {
						log.error("开盘完成:" + betType.toString() + ",耗时:"
								+ (System.currentTimeMillis() - t) + "ms");
					}
					break;
				}
				default:
					break;
				}
				log.debug("赌场当前状态:" + betRoomState.toString());
			} catch (Exception e) {
				e.printStackTrace();
				changeBetState(MmochatBetState.未启动状态);
				log.error("赌场异常:" + e.getMessage());
			}
		}
	}

	// 重新开盘：生成赔率，初始一些值
	public void initNewBetGame() {

		// 下注情况清0
		betMap.clear();

		// 命中机率
		// 生成4个随机概率：和为100，最低10，最高45%
		// 每人先分配10%,剩下60%分配给4人
		int minPercent = 10, maxPercent = 45;
		int rateA = minPercent, rateB = minPercent, rateC = minPercent, rateD = minPercent;
		int rest = 100 - 4 * minPercent;
		int randValue = MmochatUtil.getRandomValue(Math.min(maxPercent - minPercent
				+ 1, rest + 1));// 0~rest
		rateA += randValue;
		rest -= randValue;

		randValue = MmochatUtil.getRandomValue(Math.min(maxPercent - minPercent + 1,
				rest + 1));// 0~rest
		rateB += randValue;
		rest -= randValue;

		if (rest >= maxPercent - minPercent + 1) {
			rateC += (maxPercent - minPercent + 1) / 2;
			rateD += (maxPercent - minPercent + 1) / 2;
			rest -= (maxPercent - minPercent + 1) / 2 * 2;
		}

		randValue = MmochatUtil.getRandomValue(rest + 1);// 0~rest
		rateC += randValue;
		rest -= randValue;

		rateD += rest;
		

		// 赔率=(100.0/命中机率)*(1-systemWinRate%)
		double awardA, awardB, awardC, awardD;
		awardA = (100.0 / rateA)
				* (1 - MmochatConstant.systemWinPercentInBet / 100.0);
		awardB = (100.0 / rateB)
				* (1 - MmochatConstant.systemWinPercentInBet / 100.0);
		awardC = (100.0 / rateC)
				* (1 - MmochatConstant.systemWinPercentInBet / 100.0);
		awardD = (100.0 / rateD)
				* (1 - MmochatConstant.systemWinPercentInBet / 100.0);
		awardA = ((int) (awardA * 10)) / 10.0;
		awardB = ((int) (awardB * 10)) / 10.0;
		awardC = ((int) (awardC * 10)) / 10.0;
		awardD = ((int) (awardD * 10)) / 10.0;

		betRates[0].setAward(awardA);
		betRates[0].setRate(rateA);
		betRates[1].setAward(awardB);
		betRates[1].setRate(rateB);
		betRates[2].setAward(awardC);
		betRates[2].setRate(rateC);
		betRates[3].setAward(awardD);
		betRates[3].setRate(rateD);
		log.debug("当前赔率:" + betRates[0].getAward() + "("
				+ betRates[0].getRate() + ")," + betRates[1].getAward() + "("
				+ betRates[1].getRate() + ")," + betRates[2].getAward() + "("
				+ betRates[2].getRate() + ")," + betRates[3].getAward() + "("
				+ betRates[3].getRate() + ")");
	}

	public ScheduledExecutorService getBetThread() {
		return betThread;
	}

	public void setBetThread(ScheduledExecutorService betThread) {
		this.betThread = betThread;
	}

	public MmochatBetType getBetType() {
		return betType;
	}

	public void setBetType(MmochatBetType betType) {
		this.betType = betType;
	}

	public MmochatBetRate[] getBetRates() {
		return betRates;
	}

	public void setBetRates(MmochatBetRate[] betRates) {
		this.betRates = betRates;
	}

	public Map<Integer, MmochatBetInfo> getBetMap() {
		return betMap;
	}

	public void setBetMap(Map<Integer, MmochatBetInfo> betMap) {
		this.betMap = betMap;
	}

	public Map<Integer, Integer> getPlayersInBet() {
		return playersInBet;
	}

	public void setPlayersInBet(Map<Integer, Integer> playersInBat) {
		this.playersInBet = playersInBat;
	}

	public MmochatBetState getBetRoomState() {
		return betRoomState;
	}

	public void setBetRoomState(MmochatBetState betRoomState) {
		this.betRoomState = betRoomState;
	}

	public int getBetSetMoneyTime() {
		return betSetMoneyTime;
	}

	public void setBetSetMoneyTime(int betSetMoneyTime) {
		this.betSetMoneyTime = betSetMoneyTime;
	}

	public int getBetFreezeSetMoneyTime() {
		return betFreezeSetMoneyTime;
	}

	public void setBetFreezeSetMoneyTime(int betFreezeSetMoneyTime) {
		this.betFreezeSetMoneyTime = betFreezeSetMoneyTime;
	}

	public BetStart getBetRun() {
		return betRun;
	}

	public void setBetRun(BetStart betRun) {
		this.betRun = betRun;
	}

	public int getBetNum() {
		return betNum;
	}

	public double getTotalBetMoney(int index) {
		double total = 0;
		for (MmochatBetInfo betInfo : betMap.values()) {
			Integer v = betInfo.getBetMoneyMap().get(index);
			if (v != null) {
				total += v;
			}
		}
		return total;
	}

	public long getStateChangeTick() {
		return stateChangeTick;
	}

	public void setStateChangeTick(long stateChangeTick) {
		this.stateChangeTick = stateChangeTick;
	}

	public String getBetSystemMsg() {
		String msg = "";
		int pastTime = ((int) (System.currentTimeMillis() - stateChangeTick)) / 1000;
		switch (betRoomState) {
		case 可以下注状态:
			msg += "系统:现在是下注时间，下注时间还有" + (getBetSetMoneyTime() - pastTime)
					+ "秒";
			break;
		case 冻结下注状态:
			msg += "系统:现在冻结下注，" + (getBetFreezeSetMoneyTime() - pastTime)
					+ "秒后开盘。";
			break;
		case 开盘状态:
			msg += "系统:正在开盘，请稍候。";
			break;
		case 未启动状态:
			msg += "系统:本场暂时关闭。";
			break;
		default:
			break;
		}
		return msg;
	}

	// 生成赛跑数据
	public List<MmochatBetPathSection> createPath(int winIndex) {
		List<MmochatBetPathSection> path = new ArrayList<MmochatBetPathSection>();

		for (int index = 0; index < betNum; index++) {
			MmochatBetPathSection section = new MmochatBetPathSection();
			section.setIndex(index);
			path.add(section);

			// 变速次数
			int sectionNum = MmochatUtil.getRandomValue(minTimesOfChangeSpeed,
					maxTimesOfChangeSpeed);

			// 将总长度分为sectionNum段，每段长度至少要minPathLenPerSection，
			int pathLen = totalPath;
			if (index != winIndex) {
				int minPathLen = minPathLenPerSection * maxTimesOfChangeSpeed;
				minPathLen = Math.max(minPathLen, totalPath - 100);
				pathLen = MmochatUtil
						.getRandomValue(minPathLen, totalPath - 20);
			}
			pathLen -= minPathLenPerSection * sectionNum;
			int minTotalTime = 0;
			for (int i = 0; i < sectionNum - 1; i++) {
				int extraLen = MmochatUtil.getRandomValue(pathLen + 1);
				int curLen = minPathLenPerSection + extraLen;
				int minTime = curLen / maxSpeed;
				minTotalTime += minTime;
				section.getPathList().add(curLen);
				pathLen -= extraLen;
			}
			minTotalTime += (minPathLenPerSection + pathLen) / maxSpeed;
			section.getPathList().add(minPathLenPerSection + pathLen);

			// 将总时间分为sectionNum段，每段速度不能低于minSpeed，不能高于maxSpeed
			int timeLen = totalRunTime - minTotalTime;
			for (int i = 0; i < sectionNum - 1; i++) {
				int curPathLen = section.getPathList().get(i);
				int minTime = curPathLen / maxSpeed;
				int maxTime = Math
						.min(curPathLen / minSpeed - minTime, timeLen);
				int curTime = MmochatUtil.getRandomValue(maxTime + 1);
				section.getTimeList().add(minTime + curTime);
				timeLen -= curTime;
			}
			int curPathLen = section.getPathList().get(sectionNum - 1);
			int minTime = curPathLen / maxSpeed;
			section.getTimeList().add(minTime + timeLen);
		}
		return path;
	}
}
